package com.qf.servlet;

import com.qf.entity.EmpManager;
import com.qf.service.EmpManagerService;
import com.qf.service.impl.EmpManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author zdl
 * @date 2025/1/16
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private EmpManagerService empManagerService = new EmpManagerServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        session.setAttribute("name",username);
        boolean login = empManagerService.login(username, password);
        if (login){
            System.out.println("登录成功");
            resp.sendRedirect("/list");
        }else {
            System.out.println("登录失败");
            PrintWriter out = resp.getWriter();
            out.write("<h2>账号或密码有误</h2>");
            resp.sendRedirect("/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
